package thread;

import java.util.HashSet;
import java.util.Set;

public class IsHappy {


    public boolean isHappy(int n) {
        final Set<Integer> allNums = new HashSet<>();
        return getAllNums(allNums, n);
    }

    public boolean getAllNums(Set<Integer> allNums, int n) {
        if (n == 1) {
            return true;
        }

        if (allNums.contains(n)) {
            return false;
        } else {
            allNums.add(n);
        }

        String num = String.valueOf(n);

        char[] origin = num.toCharArray();
        int result = 0;
        for (char a : origin) {
            int tempA = Integer.parseInt(String.valueOf(a));
            result += tempA * tempA;
        }

        return getAllNums(allNums, result);
    }


    public static void main(String[] args) {
        IsHappy isHappy = new IsHappy();
        System.out.println(isHappy.isHappy(10));
    }

}
